package schudel.daniel.androidspellingquiz;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

//    public final static String SPELLING_LIST_NAME = "com.example.Spelling.SPELLING_LIST_NAME";

    private Spinner                 spinnerChooseList;
    private SpellingQuizApplication app;
    private Button                  buttonTakeTest;
    private static final String TAG = "MainActivity: ";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate");
        setContentView(R.layout.activity_main);

        spinnerChooseList = (Spinner)findViewById(R.id.spinnerChooseList);
    	app               = (SpellingQuizApplication) this.getApplication();
    	buttonTakeTest    = (Button)findViewById(R.id.buttonTakeTest);
    }

    protected void onStart() {
    	super.onStart();
        Log.d(TAG, "onStart");
        addListNamesToSpinner();
    }
    
    protected void onRestart() {
        Log.d(TAG, "onRestart");
    	super.onRestart();
    }

    protected void onResume() {
        Log.d(TAG, "onResume");
    	super.onResume();
    }

    protected void onPause() {
        Log.d(TAG, "onPause");
    	super.onPause();
    }

    protected void onStop() {
        Log.d(TAG, "onStop");
    	super.onStop();
    }

    protected void onDestroy() {
        Log.d(TAG, "onDestroy");
    	super.onDestroy();
    }
    
    // note that this options menu stuff was originated in f-droid
    private static final int ABOUT = Menu.FIRST;
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        super.onCreateOptionsMenu(menu);
        menu.add(Menu.NONE, ABOUT, 0, R.string.menuAbout).setIcon(android.R.drawable.ic_menu_help);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case ABOUT:
            LayoutInflater li = LayoutInflater.from(this);
            View view = li.inflate(R.layout.about, null);
  
            // Fill in the version...
            TextView tv = (TextView) view.findViewById(R.id.textViewVersion);
            PackageManager pm = getPackageManager();
            try {
                PackageInfo pi = pm.getPackageInfo(getApplicationContext().getPackageName(), 0);
                tv.setText(pi.versionName);
            } catch (Exception e) {
            }
            
            Builder p = new AlertDialog.Builder(this).setView(view);
            p.setInverseBackgroundForced(true);
            
            p.setIcon(R.drawable.ic_launcher);
            p.setTitle(getString(R.string.app_name));
            final AlertDialog alrt = p.create();
            alrt.setButton(AlertDialog.BUTTON_NEUTRAL,
                    getString(R.string.aboutWebsite),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                        	Uri uri = Uri.parse(getString(R.string.websiteAddress));
                            startActivity(new Intent(Intent.ACTION_VIEW, uri));
                        }
                    });
            alrt.setButton(AlertDialog.BUTTON_NEGATIVE, getString(R.string.buttonOK),
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,
                                int whichButton) {
                        }
                    });
            alrt.show();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void buttonOnClickTakeTest(View view) {
        String list = String.valueOf(spinnerChooseList.getSelectedItem());

    	if (true == app.prepNewQuiz(list)) {
    	    // If we have a list with words (true), then we can start the quiz
    	    Intent intent = new Intent(this, QuizActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
    	} else {
    	    // No words (false) so warn with Toast!
            Context context = getApplicationContext();
            Toast toast = Toast.makeText(context, "No words in list!", Toast.LENGTH_SHORT);
            toast.show();
    	}
    }     

    public void buttonOnClickEditLists(View view) {
        Intent intent = new Intent(this, SpellingListManagementActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        startActivity(intent);
    }
   
    private void addListNamesToSpinner() {
        DB db = new DB(this);
        ArrayList<String> listNames = db.getLists();
        
    	// TODO: find a way to clear, and add new
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listNames);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerChooseList.setAdapter(adapter);
        
        if (listNames.size() == 0) {
	    setListWidgetStatus(false);
        } else {
	    setListWidgetStatus(true);
        }
    }
    
    private void setListWidgetStatus(boolean status) {
    	buttonTakeTest.setEnabled(status);
    	spinnerChooseList.setEnabled(status);
    }
}
